package org.androidesbcn.flickr;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.REST;
import com.aetrion.flickr.Transport;
import com.aetrion.flickr.people.User;
import com.aetrion.flickr.test.TestInterface;

public class Start extends Activity {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Start.class);

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);

		Gallery g = (Gallery) this.findViewById(R.id.gallery);
		g.setAdapter(new ImageAdapter(this));

		g.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position, long id) {
				// Toast.makeText(Start.this, "" + position, Toast.LENGTH_SHORT).show();

				String apiKey = "OUR_API_KEY";
				String secret = "OUR_API_SECRET";
				Transport trans = null;
				try {
					trans = new REST();
				} catch (Exception e) {
					Log.e(this.getClass().getName(), e.getMessage());
				}
				Flickr f = new Flickr(apiKey, secret, trans);
				//TestInterface testInterface = f.getTestInterface();
				//testInterface.echo(Collections.EMPTY_LIST);
				Log.d("aqui!", "aaa");
				f.getPhotosInterface();
				TestInterface ti = f.getTestInterface();
				try {
					User user = ti.login();
					Log.d(this.getClass().getName(), "logged");

					String value = user.getRealName();
					Log.d(this.getClass().getName(), "value");

					/*
					PhotoList pl = pi.getContactsPhotos(1, false, true, true);
					Photo p = (Photo) pl.get(0);
					String value = p.getTitle();
					 */
					Toast.makeText(Start.this, value, Toast.LENGTH_LONG).show();
					// BufferedImage bi = pi.getImage(p, p.getLargeSize());

				} catch (IOException e) {
					Log.e("example", e.getMessage());
					// TODO icougil - implement
				} catch (SAXException e) {
					Log.e("example", e.getMessage());
					// TODO icougil - implement
				} catch (FlickrException e) {
					Log.e("example", e.getMessage());
					// TODO icougil - implement
					e.printStackTrace();
				}

			}
		});

	}

	public class ImageAdapter extends BaseAdapter {
		int mGalleryItemBackground;
		private Context mContext;

		private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3 };

		public ImageAdapter(Context c) {
			this.mContext = c;
			TypedArray a = Start.this.obtainStyledAttributes(R.styleable.Start);
			this.mGalleryItemBackground = a.getResourceId(
					R.styleable.Start_android_galleryItemBackground, 0);
			a.recycle();
		}

		public int getCount() {
			return this.mImageIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(this.mContext);

			i.setImageResource(this.mImageIds[position]);
			i.setLayoutParams(new Gallery.LayoutParams(150, 100));
			i.setScaleType(ImageView.ScaleType.FIT_XY);
			i.setBackgroundResource(this.mGalleryItemBackground);

			return i;
		}
	}
}